<template>
  <!-- 熔炼工单表表单弹窗 -->
  <el-dialog
    v-model="visibleDialog"
    :title="props.title"
    width="1100px"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="160">
      <el-descriptions class="margin-top" title="" :column="4" border>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">产品</div>
          </template>

          <div style="width: 100%; word-break: break-all">
            {{ formData.materialName }}
          </div>
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">熔炼工单号</div>
          </template>

          <div style="width: 100%; word-break: break-all">
            {{ formData.processingOrderNumber }}
          </div>
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">生产批号</div>
          </template>

          <div style="width: 100%; word-break: break-all">
            {{ formData.plannedBatch }}
          </div>
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">生产日期</div>
          </template>

          <div style="width: 100%; word-break: break-all">
            {{ formData.planDate }}
          </div>
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">炉号</div>
          </template>

          <div style="width: 100%; word-break: break-all">
            {{ formData.stoveNum }}
          </div>
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">炉重</div>
          </template>

          <div style="width: 100%; word-break: break-all">
            {{ formData.stoveCountNum }}
          </div>
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">炉数</div>
          </template>

          <div style="width: 100%; word-break: break-all">
            {{ formData.stoveCount }}
          </div>
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">计划开始时间</div>
          </template>

          <div style="width: 100%; word-break: break-all">
            {{ formData.planBeginTime }}
          </div>
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">计划结束时间</div>
          </template>

          <div style="width: 100%; word-break: break-all">
            {{ formData.planEndTime }}
          </div>
        </el-descriptions-item>
      </el-descriptions>

      <div style="margin-top: 20px">
        <el-radio-group v-model="radio2" @change="changeRodi">
          <el-radio-button label="首料" value="首料" />
          <el-radio-button
            :disabled="formData.listRecord.length == 1"
            label="一调质"
            value="一调质"
          />
          <el-radio-button
            :disabled="formData.listRecord.length != 3"
            label="二调质"
            value="二调质"
          />
        </el-radio-group>
      </div>

      <!-- 主料数据第一行 -->
      <el-row>
        <!-- 主料数据第一行 -->
        <el-col :span="radio2 == '首料' ? 7 : 11">
          <div
            style="
              display: flex;
              justify-content: space-between;
              align-items: center;
            "
            v-if="!props.isReadonly"
          >
            <div class="mb-[10px] mt-[20px] text-[14px] font-700">主料数据</div>
          </div>
          <div
            class="formTable"
            :class="
              (radio2 == '一调质' && formData.listRecord.length == 2) ||
              (radio2 == '二调质' && formData.listRecord.length == 3)
                ? 'inputTable'
                : ''
            "
          >
            <el-table
              :data="
                radio2 == '首料'
                  ? formData.listRecord[0]?.details
                  : radio2 == '一调质'
                  ? formData.listRecord[1]?.details
                  : formData.listRecord[2]?.details
              "
              max-height="250px"
              style="width: 100%"
              :key="Math.random"
              :header-cell-style="{ textAlign: 'center' }"
              border
            >
              <el-table-column
                label="材料名称"
                props="materialCode"
                min-width="100"
                :filter-method="filterHandler"
                :filtered-value="defaultFilter"
              >
                <template #default="{ $index, row }">
                  <div
                    @click="openDialog(row)"
                    class="cursor-pointer color-blue"
                    style="text-decoration: underline"
                  >
                    {{ row.materialName }}
                  </div>
                </template>
              </el-table-column>
              <el-table-column
                label="目标值kg"
                props="targetValue"
                min-width="100"
                align="right"
                :filters="[]"
                :filter-method="filterHandler"
              >
                <template #default="{ $index, row }">
                  <span
                    >{{ row.targetValue }}/{{
                      Number(row.targetValue) * Number(formData.stoveCount)
                    }}</span
                  >
                </template>
              </el-table-column>
              <el-table-column
                label="实际值kg"
                props="actualValue"
                min-width="100"
                align="right"
                :filters="[]"
                :filter-method="filterHandler"
              >
                <template #default="{ $index, row }">
                  <span>{{ row.actualValue }}</span>
                </template>
              </el-table-column>

              <el-table-column
                label="调整值kg"
                props="adjustValue"
                min-width="100"
                align="right"
                :filters="[]"
                :filter-method="filterHandler"
                v-if="radio2 == '一调质' && formData.listRecord.length == 3"
              >
                <template #default="{ $index, row }">
                  {{ row.adjustValue }}
                </template>
              </el-table-column>
              <el-table-column
                label="调整值kg"
                props="adjustValue"
                min-width="100"
                align="right"
                v-if="
                  (radio2 == '一调质' && formData.listRecord.length == 2) ||
                  (radio2 == '二调质' && formData.listRecord.length == 3)
                "
                :filters="[]"
                :filter-method="filterHandler"
              >
                <template #default="{ $index, row }">
                  <el-form-item
                    :prop="
                      `${
                        radio2 == '首料'
                          ? formData.listRecord[0]?.details
                          : radio2 == '一调质'
                          ? formData.listRecord[1]?.details
                          : formData.listRecord[2]?.details
                      }.` +
                      $index +
                      '.adjustValue'
                    "
                    props="adjustValue"
                    label-width="0"
                  >
                    <el-input
                      v-model="row.adjustValue"
                      placeholder="请输入"
                      clearable
                      @blur="mmChange"
                    />
                  </el-form-item>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-col>

        <!-- 回炉料数据第一行 -->
        <el-col :span="1" v-if="radio2 == '首料'"> </el-col>
        <el-col :span="7" v-if="radio2 == '首料'">
          <div
            style="
              display: flex;
              justify-content: space-between;
              align-items: center;
            "
            v-if="!props.isReadonly"
          >
            <div class="mb-[10px] mt-[20px] text-[14px] font-700">
              回炉料数据
            </div>
          </div>
          <div class="formTable">
            <el-table
              :data="
                radio2 == '首料'
                  ? formData.listRecord[0]?.details
                  : radio2 == '一调质'
                  ? formData.listRecord[1]?.details
                  : formData.listRecord[2]?.details
              "
              max-height="250px"
              style="width: 100%"
              :key="Math.random"
              :header-cell-style="{ textAlign: 'center' }"
              border
            >
              <el-table-column
                label="材料名称"
                props="materialName"
                min-width="100"
                :filter-method="filterHandler2"
                :filtered-value="defaultFilter"
              >
                <template #default="{ $index, row }">
                  <div
                    @click="openDialog(row)"
                    class="cursor-pointer color-blue"
                    style="text-decoration: underline"
                  >
                    {{ row.materialName }}
                  </div>
                </template>
              </el-table-column>
              <el-table-column
                label="目标值kg"
                props="targetValue"
                min-width="100"
                align="right"
                :filters="[]"
                :filter-method="filterHandler2"
              >
                <template #default="{ $index, row }">
                  <span
                    >{{ row.targetValue }}/{{
                      Number(row.targetValue) * Number(formData.stoveCount)
                    }}</span
                  >
                </template>
              </el-table-column>
              <el-table-column
                label="实际值kg"
                props="actualValue"
                min-width="100"
                align="right"
                :filters="[]"
                :filter-method="filterHandler2"
              >
                <template #default="{ $index, row }">
                  <span>{{ row.actualValue }}</span>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-col>
        <!-- 残汤第一行 -->
        <el-col :span="1"> </el-col>
        <el-col :span="7" v-if="radio2 == '首料'">
          <div
            style="
              display: flex;
              justify-content: space-between;
              align-items: center;
            "
            v-if="!props.isReadonly"
          >
            <div class="mb-[10px] mt-[20px] text-[14px] font-700">残汤</div>
          </div>
          <div class="formTable">
            <el-table
              :data="
                radio2 == '首料'
                  ? formData.listRecord[0]?.smtStoveSoupRecordDTO
                      ?.smtAssayRecordDTO?.details
                  : radio2 == '一调质'
                  ? formData.listRecord[1]?.smtStoveSoupRecordDTO
                      ?.smtAssayRecordDTO?.details
                  : formData.listRecord[2]?.smtStoveSoupRecordDTO
                      ?.smtAssayRecordDTO?.details
              "
              max-height="250px"
              :span-method="objectSpanMethod"
              style="width: 100%"
              :key="Math.random"
              :header-cell-style="{ textAlign: 'center' }"
              border
            >
              <el-table-column label="产品" props="productName" min-width="100">
                <template #default="{ $index, row }">
                  <span>{{
                    radio2 == "首料"
                      ? formData.listRecord[0]?.smtStoveSoupRecordDTO
                          ?.smtAssayRecordDTO?.productName
                      : radio2 == "一调质"
                      ? formData.listRecord[1]?.smtStoveSoupRecordDTO
                          ?.smtAssayRecordDTO?.productName
                      : formData.listRecord[2]?.smtStoveSoupRecordDTO
                          ?.smtAssayRecordDTO?.productName
                  }}</span>
                </template>
              </el-table-column>
              <el-table-column
                label="元素"
                props="elementCode"
                min-width="80"
                align="right"
              >
                <template #default="{ $index, row }">
                  <span>{{ row.elementCode }}</span>
                </template>
              </el-table-column>
              <el-table-column
                label="化验值"
                props="elementAssayValue"
                min-width="80"
                align="right"
              >
                <template #default="{ $index, row }">
                  <span>{{ Big(row.elementAssayValue).times(100) }}</span>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-col>
        <!-- 辅料数据第一行 -->
        <el-col :span="11" v-if="radio2 !== '首料'">
          <div
            style="
              display: flex;
              justify-content: space-between;
              align-items: center;
            "
            v-if="!props.isReadonly"
          >
            <div class="mb-[10px] mt-[20px] text-[14px] font-700">辅料数据</div>
          </div>
          <div
            class="formTable"
            :class="
              (radio2 == '一调质' && formData.listRecord.length == 2) ||
              (radio2 == '二调质' && formData.listRecord.length == 3)
                ? 'inputTable'
                : ''
            "
          >
            <el-table
              :data="
                radio2 == '首料'
                  ? formData.listRecord[0]?.details
                  : radio2 == '一调质'
                  ? formData.listRecord[1]?.details
                  : formData.listRecord[2]?.details
              "
              max-height="250px"
              style="width: 100%"
              :key="Math.random"
              :header-cell-style="{ textAlign: 'center' }"
              border
            >
              <el-table-column
                label="材料名称"
                props="materialName"
                min-width="100"
                :filter-method="filterHandler3"
                :filtered-value="defaultFilter"
              >
                <template #default="{ $index, row }">
                  <div
                    @click="openDialog(row)"
                    class="cursor-pointer color-blue"
                    style="text-decoration: underline"
                  >
                    {{ row.materialName }}
                  </div>
                </template>
              </el-table-column>
              <el-table-column
                label="目标值kg"
                props="targetValue"
                min-width="100"
                align="right"
                :filters="[]"
                :filter-method="filterHandler3"
              >
                <template #default="{ $index, row }">
                  <span>{{ row.targetValue }}</span>
                </template>
              </el-table-column>
              <el-table-column
                label="实际值kg"
                props="actualValue"
                min-width="100"
                align="right"
                v-if="!props.isReadonly"
                :filters="[]"
                :filter-method="filterHandler3"
              >
                <template #default="{ $index, row }">
                  <span>{{ row.actualValue }}</span>
                </template>
              </el-table-column>
              <!-- <el-table-column
                label="实际值kg"
                props="actualValue"
                min-width="100"
                align="right"
                v-if="radio2 == '首料' && !props.isReadonly"
                :filters="[]"
                :filter-method="filterHandler3"
              >
                <template #default="{ $index, row }">
                  <el-form-item
                    :prop="
                      `${
                        radio2 == '首料'
                          ? formData.listRecord[0]?.details
                          : radio2 == '一调质'
                          ? formData.listRecord[1]?.details
                          : formData.listRecord[2]?.details
                      }.` +
                      $index +
                      '.actualValue'
                    "
                    label-width="0"
                  >
                    <el-input
                      v-model="row.actualValue"
                      placeholder="请输入"
                      clearable
                      @blur="blurChange"
                    />
                  </el-form-item>
                </template>
              </el-table-column> -->
              <el-table-column
                label="调整值kg"
                props="adjustValue"
                min-width="100"
                align="right"
                v-if="radio2 == '一调质' && formData.listRecord.length == 3"
                :filters="[]"
                :filter-method="filterHandler3"
              >
                <template #default="{ $index, row }">
                  <span>{{ row.adjustValue }}</span>
                </template>
              </el-table-column>
              <el-table-column
                label="调整值kg"
                props="adjustValue"
                min-width="100"
                align="right"
                v-if="
                  (radio2 == '一调质' && formData.listRecord.length == 2) ||
                  (radio2 == '二调质' && formData.listRecord.length == 3)
                "
                :filters="[]"
                :filter-method="filterHandler2"
              >
                <template #default="{ $index, row }">
                  <el-form-item
                    :prop="
                      `${
                        radio2 == '首料'
                          ? formData.listRecord[0]?.details
                          : radio2 == '一调质'
                          ? formData.listRecord[1]?.details
                          : formData.listRecord[2]?.details
                      }.` +
                      $index +
                      '.adjustValue'
                    "
                    label-width="0"
                  >
                    <el-input
                      v-model="row.adjustValue"
                      required
                      placeholder="请输入"
                      clearable
                      @blur="mmChange"
                    />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                label="影响元素"
                props="materialQuality"
                min-width="100"
                :filters="[]"
                :filter-method="filterHandler2"
              >
                <template #default="{ $index, row }">
                  <span>{{ row.materialQuality }}</span>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-col>
      </el-row>
      <!-- ---------------------------------------------------------------------------- -->
      <el-row>
        <!-- 辅料数据第二行 -->
        <el-col :span="11" v-if="radio2 == '首料'">
          <div
            style="
              display: flex;
              justify-content: space-between;
              align-items: center;
            "
            v-if="!props.isReadonly"
          >
            <div class="mb-[10px] mt-[20px] text-[14px] font-700">辅料数据</div>
          </div>
          <div
            class="formTable"
            :class="
              radio2 == '首料'
                ? formData.listRecord[0]?.smtAssayRecordDTO?.details.length > 0
                : radio2 == '一调质'
                ? formData.listRecord[1]?.smtAssayRecordDTO?.details.length > 0
                : formData.listRecord[2]?.smtAssayRecordDTO?.details.length >
                    0 && !props.isReadonly
                ? 'inputTable'
                : ''
            "
          >
            <el-table
              :data="
                radio2 == '首料'
                  ? formData.listRecord[0]?.details
                  : radio2 == '一调质'
                  ? formData.listRecord[1]?.details
                  : formData.listRecord[2]?.details
              "
              style="width: 100%"
              :key="Math.random"
              max-height="250px"
              :header-cell-style="{ textAlign: 'center' }"
              border
            >
              <el-table-column
                label="材料名称"
                props="materialName"
                min-width="100"
                :filter-method="filterHandler3"
                :filtered-value="defaultFilter"
              >
                <template #default="{ $index, row }">
                  <div>
                    {{ row.materialName }}
                  </div>
                </template>
              </el-table-column>
              <el-table-column
                label="计算值kg"
                props="targetValue"
                min-width="100"
                align="right"
                :filters="[]"
                :filter-method="filterHandler3"
              >
                <template #default="{ $index, row }">
                  <span>{{ row.targetValue }}</span>
                </template>
              </el-table-column>

              <el-table-column
                label="实际值kg"
                props="actualValue"
                min-width="100"
                align="right"
                v-if="
                  props.isReadonly || radio2 == '首料'
                    ? formData.listRecord[0]?.smtAssayRecordDTO?.details
                        .length > 0
                    : radio2 == '一调质'
                    ? formData.listRecord[1]?.smtAssayRecordDTO?.details
                        .length > 0
                    : formData.listRecord[2]?.smtAssayRecordDTO?.details
                        .length > 0
                "
                :filters="[]"
                :filter-method="filterHandler3"
              >
                <template #default="{ $index, row }">
                  <span>{{ row.actualValue }}</span>
                </template>
              </el-table-column>
              <el-table-column
                label="实际值kg"
                props="actualValue"
                min-width="100"
                align="right"
                :filters="[]"
                v-if="
                  !props.isReadonly || radio2 == '首料'
                    ? formData.listRecord[0]?.smtAssayRecordDTO == null
                    : radio2 == '一调质'
                    ? formData.listRecord[1]?.smtAssayRecordDTO == null
                    : formData.listRecord[2]?.smtAssayRecordDTO == null
                "
                :filter-method="filterHandler3"
              >
                <template #default="{ $index, row }">
                  <el-form-item
                    :prop="
                      `${
                        radio2 == '首料'
                          ? formData.listRecord[0]?.details
                          : radio2 == '一调质'
                          ? formData.listRecord[1]?.details
                          : formData.listRecord[2]?.details
                      }.` +
                      $index +
                      '.actualValue'
                    "
                    label-width="0"
                    :rules="rules.actualValue"
                  >
                    <el-input
                      v-model="row.actualValue"
                      required
                      placeholder="请输入"
                      clearable
                      @blur="blurChange"
                    />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                label="影响元素"
                props="materialQuality"
                min-width="100"
                :filters="[]"
                :filter-method="filterHandler2"
              >
                <template #default="{ $index, row }">
                  <span>{{ row.materialQuality }}</span>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-col>
        <el-col :span="1"> </el-col>
        <!-- 成分含量第二行 -->
        <el-col :span="radio2 == '首料' ? 11 : 11" v-if="radio2 == '首料'">
          <div
            style="
              display: flex;
              justify-content: space-between;
              align-items: center;
            "
            v-if="!props.isReadonly"
          >
            <div class="mb-[10px] mt-[20px] text-[14px] font-700">成分含量</div>
          </div>
          <div class="formTable">
            <el-table
              :data="
                radio2 == '首料'
                  ? formData.listRecord[0]?.listCompute
                  : radio2 == '一调质'
                  ? formData.listRecord[0]?.smtAssayRecordDTO?.details
                  : formData.listRecord[1]?.smtAssayRecordDTO?.details
              "
              style="width: 100%"
              max-height="250px"
              :key="Math.random"
              :header-cell-style="{ textAlign: 'center' }"
              border
            >
              <el-table-column label="元素" props="elementName" min-width="100">
                <template #default="{ $index, row }">
                  <span>{{ row.elementName }}</span>
                </template>
              </el-table-column>
              <el-table-column
                label="标准值%"
                props="elementNormValue"
                min-width="100"
                align="right"
              >
                <template #default="{ $index, row }">
                  <span> {{ Big(row.elementNormValue)?.times(100) }}</span>
                </template>
              </el-table-column>
              <el-table-column
                label="化验值%"
                props="elementNormValue"
                min-width="100"
                align="right"
                v-if="radio2 != '首料'"
              >
                <template #default="{ $index, row }">
                  <span>{{ Big(row.elementNormValue)?.times(100) }}</span>
                </template>
              </el-table-column>
              <el-table-column
                label="配算值%"
                props="elementValue"
                min-width="100"
                align="right"
              >
                <template #default="{ $index, row }">
                  <span>{{
                    row.elementValue ? Big(row.elementValue)?.times(100) : 0
                  }}</span>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-col>

        <!-- 一调质 二调质 -->

        <el-col :span="24" v-if="radio2 == '一调质' || radio2 == '二调质'">
          <div
            style="
              display: flex;
              justify-content: space-between;
              align-items: center;
            "
            v-if="!props.isReadonly"
          >
            <div class="mb-[10px] mt-[20px] text-[14px] font-700">成分含量</div>
          </div>
          <div class="formTable">
            <el-table
              :data="
                radio2 == '一调质'
                  ? formData.listRecord[0]?.smtAssayRecordDTO?.details
                  : formData.listRecord[1]?.smtAssayRecordDTO?.details
              "
              style="width: 100%"
              max-height="250px"
              :key="Math.random"
              :header-cell-style="{ textAlign: 'center' }"
              border
            >
              <el-table-column label="元素" props="elementCode" min-width="100">
                <template #default="{ $index, row }">
                  <span>{{ row.elementCode }}</span>
                </template>
              </el-table-column>
              <el-table-column
                label="标准值%"
                props="elementStandardValue"
                min-width="100"
                align="right"
              >
                <template #default="{ $index, row }">
                  <span> {{ Big(row.elementStandardValue)?.times(100) }}</span>
                </template>
              </el-table-column>
              <el-table-column
                label="化验值%"
                props="elementAssayValue"
                min-width="100"
                align="right"
              >
                <template #default="{ $index, row }">
                  <span>{{ Big(row.elementAssayValue)?.times(100) }}</span>
                </template>
              </el-table-column>
              <el-table-column
                label="配算值%"
                props="elementValue"
                min-width="100"
                align="right"
              >
                <template #default="{ $index, row }">
                  <span>{{
                    row.elementValue ? Big(row.elementValue)?.times(100) : 0
                  }}</span>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="
            (!props.isReadonly &&
              radio2 == '首料' &&
              formData?.listRecord.length == 1) ||
            (!props.isReadonly &&
              radio2 == '一调质' &&
              formData?.listRecord.length == 2 &&
              formData?.listRecord[0]?.smtAssayRecordDTO?.details?.length >
                0) ||
            (!props.isReadonly &&
              radio2 == '二调质' &&
              formData?.listRecord.length == 3 &&
              formData?.listRecord[1]?.smtAssayRecordDTO?.details?.length > 0)
          "
          type="primary"
          :loading="loading"
          @click="handleSubmit('确定')"
          >确 定</el-button
        >
        <!-- <el-button
          v-if="
            !props.isReadonly &&
            radio2 == '一调质' &&
            formData.listRecord.length == 2
          "
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button
          v-if="
            !props.isReadonly &&
            radio2 == '二调质' &&
            formData.listRecord.length == 3
          "
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        > -->

        <el-button
          v-if="
            (!props.isReadonly &&
              radio2 == '首料' &&
              formData?.listRecord.length == 1) ||
            (!props.isReadonly &&
              radio2 == '一调质' &&
              formData?.listRecord.length == 2 &&
              formData?.listRecord[0]?.smtAssayRecordDTO?.details?.length >
                0) ||
            (!props.isReadonly &&
              radio2 == '二调质' &&
              formData?.listRecord.length == 3 &&
              formData?.listRecord[1]?.smtAssayRecordDTO?.details?.length > 0)
          "
          type="primary"
          :loading="loading"
          @click="handleSubmit('保存')"
          >保 存</el-button
        >
        <!-- <el-button
          v-if="
            !props.isReadonly &&
            radio2 == '一调质' &&
            formData.listRecord.length == 2
          "
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >保 存</el-button
        >
        <el-button
          v-if="
            !props.isReadonly &&
            radio2 == '二调质' &&
            formData.listRecord.length == 3
          "
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >保 存</el-button
        > -->
        <el-button
          v-if="
            (!props.isReadonly &&
              radio2 == '首料' &&
              formData?.listRecord.length == 1) ||
            (!props.isReadonly &&
              radio2 == '一调质' &&
              formData?.listRecord.length == 2)
          "
          type="primary"
          :loading="loading"
          :disabled="
            formData?.listRecord[0]?.smtAssayRecordDTO?.details.length < 0 ||
            formData?.listRecord[1]?.smtAssayRecordDTO?.details.length < 0
          "
          @click="handleAddSubmit"
          >加调质</el-button
        >
        <!-- <el-button
          v-if="
            !props.isReadonly &&
            radio2 == '二调质' &&
            formData.listRecord.length == 2
          "
          type="primary"
          :disabled="
            formData.listRecord[1].smtAssayRecordDTO?.details.length < 0
          "
          :loading="loading"
          @click="handleAddSubmit"
          >加调质</el-button
        > -->
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>

    <!-- <materialData
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    /> -->
    <materialAnalysis-show
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
import Big from "big.js";
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "MESSMTORDERADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    console.log(props.isReadonly);

    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);

const rules = reactive({
  sort: [],
  orderCode: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  batchNo: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  productCode: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  productName: [
    { min: 1, max: 90, message: "长度在 1 到 90 个字符", trigger: "blur" },
  ],
  planId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  planCode: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  planDate: [],
  deviceId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  deviceCode: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  planStatus: [],
  meltingStatus: [],
  stoveNumber: [],
  producePlanStarttime: [],
  producePlanEndtime: [],
  productActualStarttime: [],
  productActualEndtime: [],
  enabled: [],
  deleted: [],
  createTime: [],
  createdUserId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  createdUserName: [
    { min: 1, max: 90, message: "长度在 1 到 90 个字符", trigger: "blur" },
  ],
  lastUpdatedUserId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  lastUpdatedUserName: [
    { min: 1, max: 90, message: "长度在 1 到 90 个字符", trigger: "blur" },
  ],
  lastUpdateTime: [],

  actualValue: [
    { required: true, message: "请输入有效的正数", trigger: "blur" },
    {
      pattern: /^[1-9]\d*(\.\d+)?$/,
      message: "请输入有效的正数",
      trigger: "blur",
    },
  ],
  adjustValue: [
    { required: true, message: "请输入有效的正数", trigger: "blur" },
    {
      pattern: /^[1-9]\d*(\.\d+)?$/,
      message: "请输入有效的正数",
      trigger: "blur",
    },
  ],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  createTime: "",
  lastUpdateTime: "",
  createdUserId: "",
  lastUpdatedUserId: "",
  createdUserName: "",
  lastUpdatedUserName: "",
  organizationId: "",
  organizationCode: "",
  plantId: "",
  organizationName: "",
  plantCode: "",
  plantName: "",
  monthPlanId: "",
  processesId: "",
  processesClassification: "",
  workSequence: "",
  figureNumber: "",
  planQuantity: "",
  plannedBatch: "",
  finishQuantity: "",
  validQuantity: "",
  invalidQuantity: "",
  materialId: "",
  materialCode: "",
  materialName: "",
  scrapQuantity: "",
  quantityUnit: "",
  isClosed: "",
  isPause: "",
  formStatus: "",
  finishTime: "",
  finishUser: "",
  approveTime: "",
  approveUser: "",
  processingOrderNumber: "",
  processingOrderType: "",
  shiftSystemId: "",
  shiftId: "",
  groupsId: "",
  planUserIds: "",
  planUsers: "",
  deviceId: "",
  deviceName: "",
  demandPeople: "",
  planBeginTime: "",
  planEndTime: "",
  beginTime: "",
  endTime: "",
  routingAllocationId: "",
  planDate: "",
  planDateId: "",
  stoveNum: "",
  stoveStatus: "",
  stoveCount: "",
  workCenterId: "",
  workCenterCode: "",
  workCenterName: "",
  version: "",
  remarks: "",
  deleted: "",
  ids: "",
  shiftName: "",
  stoveCountNum: "",
  pm: "",
  ms: "",
  shiftSet: "",
  tactYield: "",
  coreState: "",
  equipModel: "",
  sandModel: "",
  sandCoreNumber: "",
  tactTime: "",
  mdmMaterialDTO: "",
  listRecord: [],
  sequence: "",
  add: false,
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        createTime,
        lastUpdateTime,
        createdUserId,
        lastUpdatedUserId,
        createdUserName,
        lastUpdatedUserName,
        organizationId,
        organizationCode,
        plantId,
        organizationName,
        plantCode,
        plantName,
        monthPlanId,
        processesId,
        processesClassification,
        workSequence,
        figureNumber,
        planQuantity,
        plannedBatch,
        finishQuantity,
        validQuantity,
        invalidQuantity,
        materialId,
        materialCode,
        materialName,
        scrapQuantity,
        quantityUnit,
        isClosed,
        isPause,
        formStatus,
        finishTime,
        finishUser,
        approveTime,
        approveUser,
        processingOrderNumber,
        processingOrderType,
        shiftSystemId,
        shiftId,
        groupsId,
        planUserIds,
        planUsers,
        deviceId,
        deviceName,
        demandPeople,
        planBeginTime,
        planEndTime,
        beginTime,
        endTime,
        routingAllocationId,
        planDate,
        planDateId,
        stoveNum,
        stoveStatus,
        stoveCount,
        workCenterId,
        workCenterCode,
        workCenterName,
        version,
        remarks,
        deleted,
        ids,
        shiftName,
        stoveCountNum,
        pm,
        ms,
        shiftSet,
        tactYield,
        coreState,
        equipModel,
        sandModel,
        sandCoreNumber,
        tactTime,
        mdmMaterialDTO,
        listRecord,
        sequence,
        add,
      } = newVal;
      Object.assign(formData, {
        id,
        createTime,
        lastUpdateTime,
        createdUserId,
        lastUpdatedUserId,
        createdUserName,
        lastUpdatedUserName,
        organizationId,
        organizationCode,
        plantId,
        organizationName,
        plantCode,
        plantName,
        monthPlanId,
        processesId,
        processesClassification,
        workSequence,
        figureNumber,
        planQuantity,
        plannedBatch,
        finishQuantity,
        validQuantity,
        invalidQuantity,
        materialId,
        materialCode,
        materialName,
        scrapQuantity,
        quantityUnit,
        isClosed,
        isPause,
        formStatus,
        finishTime,
        finishUser,
        approveTime,
        approveUser,
        processingOrderNumber,
        processingOrderType,
        shiftSystemId,
        shiftId,
        groupsId,
        planUserIds,
        planUsers,
        deviceId,
        deviceName,
        demandPeople,
        planBeginTime,
        planEndTime,
        beginTime,
        endTime,
        routingAllocationId,
        planDate,
        planDateId,
        stoveNum,
        stoveStatus,
        stoveCount,
        workCenterId,
        workCenterCode,
        workCenterName,
        version,
        remarks,
        deleted,
        ids,
        shiftName,
        stoveCountNum,
        pm,
        ms,
        shiftSet,
        tactYield,
        coreState,
        equipModel,
        sandModel,
        sandCoreNumber,
        tactTime,
        mdmMaterialDTO,
        listRecord,
        sequence,
        add,
      });
      // computeCalculate();
      hh();
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();

  formData.id = "";
  formData.createTime = "";
  formData.lastUpdateTime = "";
  formData.createdUserId = "";
  formData.lastUpdatedUserId = "";
  formData.createdUserName = "";
  formData.lastUpdatedUserName = "";
  formData.organizationId = "";
  formData.organizationCode = "";
  formData.plantId = "";
  formData.organizationName = "";
  formData.plantCode = "";
  formData.plantName = "";
  formData.monthPlanId = "";
  formData.processesId = "";
  formData.processesClassification = "";
  formData.workSequence = "";
  formData.figureNumber = "";
  formData.planQuantity = "";
  formData.plannedBatch = "";
  formData.finishQuantity = "";
  formData.validQuantity = "";
  formData.invalidQuantity = "";
  formData.materialId = "";
  formData.materialCode = "";
  formData.materialName = "";
  formData.scrapQuantity = "";
  formData.quantityUnit = "";
  formData.isClosed = "";
  formData.isPause = "";
  formData.formStatus = "";
  formData.finishTime = "";
  formData.finishUser = "";
  formData.approveTime = "";
  formData.approveUser = "";
  formData.processingOrderNumber = "";
  formData.processingOrderType = "";
  formData.shiftSystemId = "";
  formData.shiftId = "";
  formData.groupsId = "";
  formData.planUserIds = "";
  formData.planUsers = "";
  formData.deviceId = "";
  formData.deviceName = "";
  formData.demandPeople = "";
  formData.planBeginTime = "";
  formData.planEndTime = "";
  formData.beginTime = "";
  formData.endTime = "";
  formData.routingAllocationId = "";
  formData.planDate = "";
  formData.planDateId = "";
  formData.stoveNum = "";
  formData.stoveStatus = "";
  formData.stoveCount = "";
  formData.workCenterId = "";
  formData.workCenterCode = "";
  formData.workCenterName = "";
  formData.version = "";
  formData.remarks = "";
  formData.deleted = "";
  formData.ids = "";
  formData.shiftName = "";
  formData.stoveCountNum = "";
  formData.pm = "";
  formData.ms = "";
  formData.shiftSet = "";
  formData.tactYield = "";
  formData.coreState = "";
  formData.equipModel = "";
  formData.sandModel = "";
  formData.sandCoreNumber = "";
  formData.tactTime = "";
  formData.mdmMaterialDTO = "";
  formData.listRecord = [];
  formData.sequence = "";
  formData.add = false;
  radio2.value = "首料";
}

const loading = ref(false);

/** 熔炼工单表保存提交 */
async function handleSubmit(string) {
  // formRef.value.validate(async (valid: any) => {
  //   if (valid) {

  loading.value = true;
  const text = formData.id ? "编辑" : "新增";

  if (string == "保存") {
    formData.stoveStatus = "1";
  } else if (string == "确定") {
    formData.stoveStatus = "2";
  }
  console.log("formData", formData);
  await request({
    url: "/mes/mesplanworkingform/saveSmelt",
    method: "post",
    data: formData,
  }).finally(() => {
    loading.value = false;
  });
  ElMessage.success(text + `成功`);
  closeDialog();
  emit("reset-table", formData.id);
  //   }
  // });
}
// 加调质
async function handleAddSubmit() {
  // formRef.value.validate(async (valid: any) => {
  // if (valid) {
  console.log("formData", formData);
  loading.value = true;
  formData.add = true;
  request({
    url: `/mes/mesplanworkingform/querySmelt`,
    method: "post",
    data: {
      id: formData.id,
      isAdd: true,
      sequence: radio2.value == "首料" ? 1 : radio2.value == "一调质" ? 2 : "",
    },
  })
    .then((res) => {
      formData.listRecord = res.data.listRecord;
      // console.log("formData.listRecord", formData.listRecord);
    })
    .finally(() => {
      loading.value = false;
    });
  ElMessage.success(`成功`);

  // }
  // });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});

const radio2 = ref("首料");

const list1 = ref([]);
const list2 = ref([]);
const list3 = ref([]);

watch(
  () => radio2.value,
  (newVal: string) => {
    if (newVal == "首料") {
      list1.value = formData.listRecord[0].details;
    }
  },
  { deep: true }
);
// 查看

async function getInfo1(id: string) {
  request({
    url: `/mes/mesplanworkingform/get/${id}`,
    method: "get",
  })
    .then((data) => {
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});
/** 打开熔炼工单表表单弹窗 */
function openDialog(roleObj?: Object) {
  if (roleObj) {
    addDialog.title = "原材料化验数据";
    console.log(roleObj);

    // getInfo1(roleObj.id);
    addDialog.data = roleObj;
    addDialog.readvisible = true;
    addDialog.visible = true;
  }
}
function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}
// 获取辅料数据
const getQueryListByAuxiliary = async (e) => {
  await request({
    url: "/mdm/mdmmaterial/queryListByAuxiliary",
    method: "post",
    data: { plantId },
  });
};

// 主料、辅料、炉料数据
function filterHandler(value, row, column) {
  return row.feedingType === 1;
}
function filterHandler2(value, row, column) {
  return row.feedingType === 2;
}
function filterHandler3(value, row, column) {
  return row.feedingType === 3;
}
// 残汤 合并
const objectSpanMethod = ({ row, column, rowIndex, columnIndex }) => {
  let mm =
    radio2.value == "首料"
      ? formData.listRecord[0]?.smtStoveSoupRecordDTO?.smtAssayRecordDTO
          ?.details.length
      : radio2.value == "一调质"
      ? formData.listRecord[1]?.smtStoveSoupRecordDTO?.smtAssayRecordDTO
          ?.details.length
      : formData.listRecord[2]?.smtStoveSoupRecordDTO?.smtAssayRecordDTO
          ?.details.length;

  if (columnIndex === 0) {
    // 仅处理第一列（产品列）
    // 如果是第一行，合并所有行；其他行不显示
    return rowIndex === 0
      ? [mm, 1] // [rowspan, colspan]
      : [0, 0]; // 隐藏其他行
  }
  // 其他列保持正常
  return [1, 1];
};

let defaultFilter = reactive(["Beijing"]);

// 计算接口
const hh = async () => {
  await request({
    url: "/mes/smtmaterialrecord/calculationValue",
    method: "post",
    data:
      radio2.value == "首料"
        ? formData.listRecord[0]
        : radio2.value == "一调质"
        ? formData.listRecord[1]
        : formData.listRecord[2],
  }).then((res) => {
    console.log(res.data);

    formData.listRecord[
      radio2.value == "首料" ? 0 : radio2.value == "一调质" ? 1 : 2
    ] = res.data;

    formData.listRecord[
      radio2.value == "首料" ? 0 : radio2.value == "一调质" ? 1 : 2
    ]?.listCompute?.forEach((item) => {
      // 赋值 item.elementValue
      item.elementValue = Big(
        formData.listRecord[
          radio2.value == "首料" ? 0 : radio2.value == "一调质" ? 1 : 2
        ].mapCalculation[item.elementName]
      )
        .div(
          formData.listRecord[
            radio2.value == "首料" ? 0 : radio2.value == "一调质" ? 1 : 2
          ].feedingWeight
        )
        .toFixed(4);
    });
  });
  formRef.value.resetFields();
  formRef.value.clearValidate();
};

const mmChange = () => {
  mm();
};

const mm = async () => {
  await request({
    url: "/mes/smtmaterialrecord/calculate",
    method: "post",
    data:
      radio2.value == "首料"
        ? formData.listRecord[0]
        : radio2.value == "一调质"
        ? formData.listRecord[1]
        : formData.listRecord[2],
  }).then((res) => {
    console.log(res.data);

    // console.log(res.data);
    //  radio2.value == "一调质"

    formData.listRecord[
      radio2.value == "一调质" ? 0 : radio2.value == "二调质" ? 1 : 1
    ].smtAssayRecordDTO?.details?.forEach((item) => {
      console.log(item);

      for (let key in res.data) {
        console.log(key);

        if (item.elementCode == key) {
          item.elementValue = res.data[key];
        }
      }
    });

    // if(item.elementCode==res.data[item.elementCode]){
    //   item.elementValue=
    // }
    console.log(
      formData.listRecord[
        radio2.value == "一调质" ? 0 : radio2.value == "二调质" ? 1 : 1
      ].smtAssayRecordDTO?.details
    );
  });

  //   formData.listRecord[
  //     radio2.value == "首料" ? 0 : radio2.value == "一调质" ? 1 : 2
  //   ]?.listCompute?.forEach((item) => {
  //     // 赋值 item.elementValue
  //     item.elementValue = Big(
  //       formData.listRecord[
  //         radio2.value == "首料" ? 0 : radio2.value == "一调质" ? 1 : 2
  //       ].mapCalculation[item.elementName]
  //     )
  //       .div(
  //         formData.listRecord[
  //           radio2.value == "首料" ? 0 : radio2.value == "一调质" ? 1 : 2
  //         ].feedingWeight
  //       )
  //       .toFixed(4);
  //   });

  formRef.value.resetFields();
  formRef.value.clearValidate();
};

const changeRodi = (e) => {
  if (formData.listRecord.length == 2 && e == "首料") {
    return;
  } else if (formData.listRecord.length == 2 && e == "一调质") {
    mm();
  } else if (formData.listRecord.length == 3 && e == "一调质") {
    return;
  } else if (formData.listRecord.length == 3 && e == "二调质") {
    // mm();
  }
};
// 鼠标移开

const blurChange = () => {
  hh();
};
</script>
<style lang="scss" scoped>
.formTable {
  :deep(.el-form-item) {
    margin: 0 0 13px 0 !important;
    width: 100%;
  }
}

:deep(.formTable .el-table .el-form-item) {
  padding: 11px 0 0 0 !important;
}
:deep(.formTable .el-table .el-form-item__error) {
  font-size: 11px !important;
}

:deep(.inputTable .el-table) {
  .el-table__body td.el-table__cell {
    // padding: 5px !important;
    padding: 0 !important;
  }
}

// :deep(.el-dialog .el-dialog__body) {
//   overflow-y: auto;
//   height: 65vh;
// }

// :deep(.search-container .el-input.el-input--default.el-input--suffix) {
//   // 固定宽度
//   width: 190px !important;
// }

// :deep(.search-container .el-form-item--default .el-form-item__label) {
//   // 固定宽度
//   justify-content: end;
// }

:deep(.el-descriptions__label.el-descriptions__cell.is-bordered-label) {
  min-width: 90px;
}
:deep(.el-descriptions__content.el-descriptions__cell.is-bordered-content) {
  min-width: 160px;
}

:deep(.el-table__column-filter-trigger) {
  display: none;
}
</style>
